class TicketController < ApplicationController
  
  layout 'ipm'
  def show
    @ticket = Ticket.find(params[:id])
    @tags = Ticket.tag_counts(:limit => 4 , :order => 'count desc', :conditions => ["Taggings.taggable_id IN (SELECT id FROM tickets WHERE project_id=" + @ticket.project.id.to_s + ")"])
    @tag_list = @ticket.tag_list
    
    @title = @ticket.project.name + "::" + @ticket.name
    #page title
    @header_title = @ticket.project.name
    #sub menu
    @submenu = 'ticket'
    
  if not (current_user.has_role?(@ticket.project.name + "_member") || current_user.has_role?(@ticket.project.name + "_admin"))
    flash[:error] = "Forbidden"
    redirect_to :controller => 'project', :action => 'list'
  end
    
  rescue ActiveRecord::RecordNotFound
    flash[:notice] = "Could not find ticket with ID=" + params[:id].to_s
    redirect_to :action => 'index'
  end
  
  
  def new
    @ticket = Ticket.new
    @ticket.project = Project.find(params[:project])
    @ticket.status = 1
    @tags = Ticket.tag_counts(:limit => 4 , :order => 'count desc', :conditions => ["Taggings.taggable_id IN (SELECT id FROM tickets WHERE project_id=" + @ticket.project.id.to_s + ")"])
    @tag_list = @ticket.tags.collect{|t| t.name.downcase }.join(", ")

    
    #page title
    @header_title = @ticket.project.name
    #sub menu
    @submenu = 'ticket'
    
    if not (current_user.has_role?(@ticket.project.name + "_member") || current_user.has_role?(@ticket.project.name + "_admin"))
      flash[:error] = "Forbidden"
      redirect_to :controller => 'project', :action => 'list'
    end
    
  rescue ActiveRecord::RecordNotFound
    flash[:notice] = "Could not find project with ID=" + params[:project].to_s
    redirect_to :action => 'index'
  end
  
  
  def create
    if request.get?
      redirect_to :action => 'index'
    else
      @ticket = Ticket.new(params[:ticket])
      @ticket.name = strip_html(@ticket.name)
      @tags = Ticket.tag_counts(:limit => 4 , :order => 'count desc', :conditions => ["Taggings.taggable_id IN (SELECT id FROM tickets WHERE project_id=" + @ticket.project.id.to_s + ")"])

      
      #page title
      @header_title = @ticket.project.name
      #sub menu
      @submenu = 'ticket'
      
      if not (current_user.has_role?(@ticket.project.name + "_member") || current_user.has_role?(@ticket.project.name + "_admin"))
        flash[:error] = "Forbidden"
        redirect_to :controller => 'project', :action => 'list'
      end
      
      
      @ticket.user_id  = session[:user]
      @ticket.updated_by_id  = session[:user]
      @ticket.tag_list = params[:tag_list]
      @tag_list = @ticket.tag_list
      params[:ticket_reply][:owner_id] = params[:ticket][:owner_id]
      params[:ticket_reply][:priority] = params[:ticket][:priority]
      params[:ticket_reply][:status] = params[:ticket][:status]
      params[:ticket_reply][:ticket_type] = params[:ticket][:ticket_type]
      params[:ticket_reply][:user_id] = session[:user]
      
      
      @ticket_reply = TicketReply.new(params[:ticket_reply])
      
      @ticket.ticket_replies.build(params[:ticket_reply])
      
      if @ticket.save
        flash[:notice] = "Ticket was created successfully."
        redirect_to :action => 'show', :id => @ticket
      else
        render :action => 'new'
      end
    end
  end  
  
  def update
    if request.get?
      redirect_to :action => 'index'
    else
      @ticket = Ticket.find(params[:ticket][:id])
      
      #page title
      @header_title = @ticket.project.name
      #sub menu
      @submenu = 'ticket'
      
      if not (current_user.has_role?(@ticket.project.name + "_member") || current_user.has_role?(@ticket.project.name + "_admin"))
        flash[:error] = "Forbidden"
        redirect_to :controller => 'project', :action => 'list'
      end
      
      
      
      @ticket.update_attributes(params[:ticket])
      @ticket.updated_by_id  = session[:user]
      
      @ticket.tag_list = params[:tag_list]
      @tag_list = @ticket.tag_list
      
      params[:ticket_reply][:owner_id] = params[:ticket][:owner_id]
      params[:ticket_reply][:priority] = params[:ticket][:priority]
      params[:ticket_reply][:status] = params[:ticket][:status]
      params[:ticket_reply][:ticket_type] = params[:ticket][:ticket_type]
      params[:ticket_reply][:user_id] = session[:user]
      
      
      @ticket.ticket_replies.build(params[:ticket_reply])
      
      if @ticket.save
        flash[:notice] = "Ticket was updated successfully."
        redirect_to :action => 'show', :id => @ticket
      else
        render :action => 'show'
      end
      
    end
  rescue ActiveRecord::RecordNotFound
    flash[:notice] = "Could not find ticket with ID=" + params[:id].to_s
    redirect_to :action => 'index'  
  end
  
  
  def edit_ticket
    @ticket = Ticket.find(params[:id])
    @ticket_reply = @ticket.ticket_replies.first
    @tags = Ticket.tag_counts(:limit => 4 , :order => 'count desc', :conditions => ["Taggings.taggable_id IN (SELECT id FROM tickets WHERE project_id=" + @ticket.project.id.to_s + ")"])
    
    @tag_list = @ticket.tag_list
    
    #page title
    @header_title = @ticket.project.name
    #sub menu
    @submenu = 'ticket'
    
    if not current_user.has_role?(@ticket.project.name + "_admin")
      flash[:error] = "Forbidden: Your not a 'project admin'"
      redirect_to :controller => 'project', :action => 'list'
    end
  end
  
  def update_ticket
    if request.get?
      redirect_to :action => 'index'
    else
      @ticket = Ticket.find(params[:id])
      
      #page title
      @header_title = @ticket.project.name
      #sub menu
      @submenu = 'ticket'
      
      if not current_user.has_role?(@ticket.project.name + "_admin")
        flash[:error] = "Forbidden: Your not a 'project admin'"
        redirect_to :controller => 'project', :action => 'list'
      end
      
      @ticket.update_attributes(params[:ticket])
      @ticket.updated_by_id  = session[:user]
      
      @ticket.tag_list = params[:tag_list]
      @tag_list = @ticket.tag_list
      @tags = Ticket.tag_counts(:limit => 4 , :order => 'count desc', :conditions => ["Taggings.taggable_id IN (SELECT id FROM tickets WHERE project_id=" + @ticket.project.id.to_s + ")"])
      
      if @ticket.save
        flash[:notice] = "Ticket was updated successfully."
        redirect_to :action => 'show', :id => @ticket
      else
        render :action => 'edit_ticket'
      end
      
    end  
  rescue ActiveRecord::RecordNotFound
     flash[:notice] = "Could not find ticket with ID=" + params[:id].to_s
     redirect_to :action => 'index'  
  end
  
  
  def edit_ticket_reply
    @ticket_reply = TicketReply.find(params[:id])
    
    #page title
    @header_title = @ticket_reply.ticket.project.name
    #sub menu
    @submenu = 'ticket'
    
    if not current_user.has_role?(@ticket_reply.ticket.project.name + "_admin")
      flash[:error] = "Forbidden: Your not a 'project admin'"
      redirect_to :controller => 'project', :action => 'list'
    end
  end
  
  def update_ticket_reply
    @ticket_reply = TicketReply.find(params[:id])
    
    #page title
    @header_title = @ticket_reply.ticket.project.name
    #sub menu
    @submenu = 'ticket'
    
    if not current_user.has_role?(@ticket_reply.ticket.project.name + "_admin")
      flash[:error] = "Forbidden: Your not a 'project admin'"
      redirect_to :controller => 'project', :action => 'list'
    end
    
    
    @ticket_reply.update_attributes(params[:ticket_reply])
    if @ticket_reply.save
      flash[:notice] = "Ticket was updated successfully."
      redirect_to :action => 'show', :id => @ticket_reply.ticket
    else
      render :action => 'edit_ticket_reply'
    end
  end
end
